package cn.lutu.adapter.controller;

import cn.lutu.domain.service.CategoryService;;
import cn.lutu.protocol.dto.CategoryAddNewParam;
import cn.lutu.protocol.entity.Category;
import cn.lutu.protocol.vo.CategoryListVO;
import cn.lutu.protocol.vo.CategoryStandardVO;
import cn.lutu.protocol.vo.CategoryTreeVO;
import cn.lutu.util.pojo.vo.PageData;
import cn.lutu.util.response.ResultVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Slf4j
@RestController
@Api(tags = "类型模块")
@RequestMapping("/v1/category/")
public class CategoryController {
    @Autowired
    CategoryService categoryService;

    @PostMapping("/addNew")
    @ApiOperation("新增类型")
    public ResultVO addNew(@Validated CategoryAddNewParam categoryAddNewParam){
        log.info("开始执行新增类型请求");
        categoryService.addNew(categoryAddNewParam);
        return ResultVO.ok();
    }

    @PostMapping("/{id}/delete")
    @ApiOperation("删除类型")
    public ResultVO deleteById(@PathVariable Long id){
        log.info("开始执行删除类型请求");
        categoryService.deleteById(id);
        return ResultVO.ok();
    }

    @PostMapping("/update")
    @ApiOperation("根据ID修改类型")
    public ResultVO updateCategory(CategoryAddNewParam categoryAddNewParam){
        log.info("开始执行根据ID修改类型的请求");
        categoryService.updateById(categoryAddNewParam);
        return ResultVO.ok();
    }

    @GetMapping("/list")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", dataType = "int"),
            @ApiImplicitParam(name = "queryType", value = "查询类型，当需要查询全部数据时，此参数值应该是all")
    })
    @ApiOperation("获取类型列表")
    public ResultVO getCategories(Integer page, String queryType){
        log.debug("开始处理【查询标签类别列表】请求，页码：{}", page);
        if (page == null) {
            page = 1;
        }
        Integer pageNum = page > 0 ? page : 1;
        PageData<CategoryListVO> pageData ;
        if ("all".equals(queryType)) {
            pageData = categoryService.getCategories(1, Integer.MAX_VALUE);
        } else {
            pageData = categoryService.getCategories(pageNum);
        }
        log.info("查询结束，查询结果为{}",pageData);
        return ResultVO.ok(pageData);
    }

    @GetMapping("/tree")
    @ApiOperation("获取类型树")
    public ResultVO getCategoryTree(Long id){
        log.info("开始执行获取类型树的请求");
        List<CategoryTreeVO> categoryTree = categoryService.getCategoryTree(id);
        return ResultVO.ok(categoryTree);
    }

    @GetMapping("/listByParentId")
    @ApiOperation("根据父级ID获取类型")
    public ResultVO getCategoriesByParentId(Long parentId){
        log.info("开始执行根据父级ID获取类型的请求");
        List<CategoryListVO> category = categoryService.getCategoryListByParentId(parentId);
        log.debug("查询到的参数为{}",category);
        return ResultVO.ok(category);
    }

    @GetMapping("/selectById")
    @ApiOperation("根据ID获取类型")
    public ResultVO getCategory(Long id){
        log.info("开始执行根据ID获取类型的请求");
        Category category = categoryService.selectById(id);
        log.debug("查询到的参数为{}",category);
        return ResultVO.ok(category);
    }

    @GetMapping("/getById")
    @ApiOperation("根据ID获取类型")
    public ResultVO getStandardCategory(Long id){
        log.info("开始执行根据ID获取类型的请求");
        CategoryStandardVO category = categoryService.selectStandardVOById(id);
        log.debug("查询到的参数为{}",category);
        return ResultVO.ok(category);
    }
}
